package com.kylin.eduservice.controller;


import com.kylin.R;
import com.kylin.eduservice.client.VodClient;
import com.kylin.eduservice.entity.EduVideo;
import com.kylin.eduservice.service.EduVideoService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author kylin
 * @since 2020-07-15
 */
@RestController
@RequestMapping("/eduservice/video")
@CrossOrigin
public class EduVideoController {


    @Autowired
    EduVideoService eduVideoService;

    @Autowired
    VodClient vodClient;

    //添加小节
    @PostMapping("/addVideo")
    @ApiOperation("添加小节")
    public R addVideo(@RequestBody EduVideo eduVideo) {
        eduVideoService.save(eduVideo);
        return R.ok();
    }

    //删除小节 后面需要完善，要连同视频一起删除
    @DeleteMapping("/{id}")
    @ApiOperation("删除小节")
    public R deleteVideo(@PathVariable String id) {
        //1.删除视频
        //根据小节id获取到视频id
        EduVideo video = eduVideoService.getById(id);
        String videoSourceId = video.getVideoSourceId();
        //有些小节可能没有视频id，进行判断。有的才删除
        if (!StringUtils.isEmpty(videoSourceId)) {

            R r = vodClient.removeAlyVideo(videoSourceId);
            if (r.getCode() == 20001) {
                System.out.println(r.getMessage());
                System.out.println("fallback");
            }
        }
        //2.删除小节
        eduVideoService.removeById(id);
        return R.ok();
    }


}

